package cn.wolfcode.web.controller;

import cn.hutool.core.map.MapUtil;
import cn.wolfcode.enums.ConsumptionReportEnum;
import cn.wolfcode.qo.ConsumptionReportQueryObject;
import cn.wolfcode.service.IBusinessService;
import cn.wolfcode.service.IConsumptionReportService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author chenhui
 * @Date 2021/1/17 10:44
 **/
@Controller
@RequestMapping("/consumptionReport")
public class ConsumptionReportController {

    @Autowired
    private IConsumptionReportService consumptionReportService;

    @Autowired
    private IBusinessService businessService;

    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") ConsumptionReportQueryObject qo){
        model.addAttribute("pageInfo",consumptionReportService.query(qo));
        //分组类型
        model.addAttribute("enums", ConsumptionReportEnum.values());
        //门店
        model.addAttribute("businesses", businessService.listAll());
        return "consumptionReport/list";
    }


    @RequestMapping("/listBar")
    public String listBar(Model model,@ModelAttribute("qo") ConsumptionReportQueryObject qo){
        //查询分组的数据,不需要分页
        qo.setPageSize(0);
        List<HashMap<String,Object>> list = consumptionReportService.query(qo).getList();
/*
//        1. 准备5个容器
        ArrayList<Object> xList = new ArrayList<>();
        ArrayList<Object> numberList = new ArrayList<>();
        ArrayList<Object> totalAmountList = new ArrayList<>();
        ArrayList<Object> payAmountList = new ArrayList<>();
        ArrayList<Object> discountAmountList = new ArrayList<>();
//        2. 遍历查询出来的集合, 获取到每个map
        for (HashMap map : list) {
            //3. 根据不同的key 获取value 放到不同的容器中
            xList.add(map.get("groupType"));
            numberList.add(map.get("number"));
            totalAmountList.add(map.get("totalAmount"));
            payAmountList.add(map.get("payAmount"));
            discountAmountList.add(map.get("discountAmount"));
        }

        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("numberList",JSON.toJSONString(numberList));
        model.addAttribute("totalAmountList",JSON.toJSONString(totalAmountList));
        model.addAttribute("payAmountList",JSON.toJSONString(payAmountList));
        model.addAttribute("discountAmountList",JSON.toJSONString(discountAmountList));
*/


        Map<String, List<Object>> kListMap = MapUtil.toListMap(list);

        model.addAttribute("xList", JSON.toJSONString(kListMap.get("groupType")));
        model.addAttribute("numberList",JSON.toJSONString(kListMap.get("number")));
        model.addAttribute("totalAmountList",JSON.toJSONString(kListMap.get("totalAmount")));
        model.addAttribute("payAmountList",JSON.toJSONString(kListMap.get("payAmount")));
        model.addAttribute("discountAmountList",JSON.toJSONString(kListMap.get("discountAmount")));

        return "consumptionReport/listBar";
    }










   /* @RequestMapping("/listBar")
    @ResponseBody
    public Map listBar(Model model,@ModelAttribute("qo") ConsumptionReportQueryObject qo){
        //查询分组的数据,不需要分页
        qo.setPageSize(0);
        List<HashMap<String,Object>> list = consumptionReportService.query(qo).getList();
        Map<String, List<Object>> kListMap = MapUtil.toListMap(list);
        return kListMap;
    }*/

}
